package com.aisile.sellergoods.service.iml;

import com.aisile.entity.PageResult;
import com.aisile.manager.BrandService;
import com.aisile.mappers.TbBrandMapper;
import com.aisile.pojo.TbBrand;
import com.aisile.pojo.TbBrandExample;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceiml implements BrandService {


    @Autowired
    TbBrandMapper tbBrandMapper;

    @Override
    public List<Map> findBrandList() {
        return tbBrandMapper.selectOptionList();
    }
    @Override
    public int delAll(Long[] ids) {
        int i=0;
        if(null!=ids){
            for(Long id:ids){
                i=tbBrandMapper.deleteByPrimaryKey(id);
            }
        }
        return i;
    }


    public List<TbBrand> findAll() {
        return tbBrandMapper.selectByExample(null);
    }

    //添加方法
    public int save(TbBrand brand) {
        int i=tbBrandMapper.insert(brand);
        return i;
    }

    @Override
    public TbBrand findOne(Long id) {
        return tbBrandMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(TbBrand tbBrand) {
        return tbBrandMapper.updateByPrimaryKey(tbBrand);
    }

    public PageResult serachList(Integer page, Integer rows, TbBrand tbBrand) {
        PageHelper.startPage(page,rows);
        TbBrandExample tbBrandExample=new TbBrandExample();
        //判断模糊条件
        if(!StringUtils.isEmpty(tbBrand.getName())){
            tbBrandExample.createCriteria().andNameLike("%"+tbBrand.getName()+"");
        }
        //判断模糊条件
        if(!StringUtils.isEmpty(tbBrand.getFirstChar())){
            tbBrandExample.createCriteria().andFirstCharLike("%"+tbBrand.getFirstChar()+"%");
        }
        //设置分页
        List<TbBrand> list=tbBrandMapper.selectByExample(tbBrandExample);
        Page<TbBrand> p=(Page<TbBrand>)list;
        return new PageResult(p.getTotal(),p.getResult());
    }
}
